package com.bao;

/**
 * @author baoye
 * @date 2024-08-08/17:00
 */
public class DieLock {
    public static void main(String[] args) {
        Object o1=new Object();
        Object o2=new Object();
        Thread thread1=new Thread(new DieRun(o1,o2),"线程1");
        Thread thread2=new Thread(new DieRun(o2,o1),"线程2");
        thread1.start();
        thread2.start();
    }
}
class DieRun implements Runnable{
    private Object object1;
    private Object object2;

    public DieRun(Object object1, Object object2) {
        this.object1 = object1;
        this.object2 = object2;
    }

    public DieRun() {
    }

    @Override
    public void run() {
        synchronized (object1){
            System.out.println(Thread.currentThread().getName()+"获取第一个对象");
            System.out.println(Thread.currentThread().getName()+"等待获取第二个对象");
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (object2){
                System.out.println(Thread.currentThread().getName()+"获取第二个对象");
            }
        }
    }
}
